package com.magicsoft.app.wcf;

import org.apache.http.Header;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.boyuan.huijiaoyu.R;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.magicsoft.app.helper.TokenHelper;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import com.magicsoft.constant.Constant;

public class SmsService extends BaseService {

	public SmsService(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 发送短信获取验证码
	 * 
	 * @param mobile
	 * @param listener
	 */
	public void sendSms(String mobile,
			final GetOneRecordListener<String> listener) {

		RequestParams params = new RequestParams();

		params.put("mobile", mobile);
		params.put("type", "0");

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.SMS + getParamString + "&d=sms";

		Log.i("sendSms", requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {

						super.onSuccess(statusCode, headers, response);
						try {
							Log.i("sendSms", response.toString());

							String code = response.getString("code");
							String message = response.getString("message");
							if ("0".equals(code)) {
								JSONObject object = response
										.getJSONObject("content");

								String status = object.getString("ok");
								if (status != null) {
									if ("1".equals(status)) {

										if (listener != null)
											listener.onFinish("");

									} else {
										if (listener != null)
											listener.onFailed(message);
									}
								} else {
									if (listener != null)
										listener.onFailed(message);
								}

							} else {
								if (listener != null)
									listener.onFailed(message);
							}

						} catch (Exception e) {

							if (listener != null)
								listener.onFailed(context.getResources()
										.getString(// JSON
													// 解析错误
												R.string.json_analysis_wrong));
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {

						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});

	}

	/**
	 * 校验短信验证码获取操作凭证
	 * 
	 * @param mobile
	 * @param listener
	 */
	public void checkSms(String mobile, String code,
			final GetOneRecordListener<String> listener) {

		RequestParams params = new RequestParams();

		params.put("mobile", mobile);
		params.put("code", code);

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.SMS + getParamString + "&d=sms";

		Log.i("checkSms", requestUrl);

		AsyncHttpServiceHelper.put(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {

						super.onSuccess(statusCode, headers, response);
						try {
							Log.i("checkSms", response.toString());

							String code = response.getString("code");
							String message = response.getString("message");
							if ("0".equals(code)) {
								JSONObject object = response
										.getJSONObject("content");

								String status = object.getString("ok");
								if (status != null) {
									if ("1".equals(status)) {

										if (listener != null)
											listener.onFinish("ok");

									} else {
										if (listener != null)
											listener.onFailed("验证码错误");
									}
								} else {
									if (listener != null)
										listener.onFailed(Constant.NO_DATA);
								}

							} else {
								if (listener != null)
									listener.onFailed(message);
							}

						} catch (Exception e) {

							if (listener != null)
								listener.onFailed(context.getResources()
										.getString(// JSON
													// 解析错误
												R.string.json_analysis_wrong));
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {

						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});

	}

}
